package com.bjsxt.controller.erp;

import com.bjsxt.aspectj.annotation.Log;
import com.bjsxt.aspectj.enums.BusinessType;
import com.bjsxt.controller.BaseController;
import com.bjsxt.dto.MedicinesDto;
import com.bjsxt.service.MedicinesService;
import com.bjsxt.utils.ShiroSecurityUtils;
import com.bjsxt.vo.AjaxResult;
import com.bjsxt.vo.DataGridView;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 药品信息控制层
 */
@RestController
@RequestMapping("erp/medicines")
public class MedicinesController extends BaseController {

    @Reference  //使用dubbo的引用
    private MedicinesService medicinesService;

    /**
     * 分页查询药品信息
     *
     * @param medicinesDto
     * @return
     */
    @GetMapping("listMedicinesForPage")
    @HystrixCommand
    public AjaxResult listMedicinesForPage(MedicinesDto medicinesDto) {
        DataGridView dataGridView = this.medicinesService.listMedicinesPage(medicinesDto);
        return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
    }

    /**
     * 添加药品信息
     *
     * @param medicinesDto
     * @return
     */
    @PostMapping("addMedicines")
    @Log(title = "添加药品信息", businessType = BusinessType.INSERT)
    @HystrixCommand
    public AjaxResult addMedicinex(@Validated MedicinesDto medicinesDto) {
        medicinesDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.medicinesService.addMedicines(medicinesDto));
    }

    /**
     * 修改药品信息
     *
     * @param medicinesDto
     * @return
     */
    @PutMapping("updateMedicines")
    @Log(title = "修改药品信息", businessType = BusinessType.UPDATE)
    @HystrixCommand
    public AjaxResult updateMedicines(@Validated MedicinesDto medicinesDto) {
        medicinesDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.medicinesService.updateMedicines(medicinesDto));
    }

    /**
     * 根据id查询一条药品信息
     *
     * @param medicinesId
     * @return
     */
    @GetMapping("getMedicinesById/{medicinesId}")
    @HystrixCommand
    public AjaxResult getMedicinesById(@Validated @PathVariable @NotNull(message = "药品medicinesId不能为空") Long medicinesId) {
        return AjaxResult.success(this.medicinesService.getOne(medicinesId));
    }

    /**
     * 根据ids删除药品信息
     *
     * @param medicinesIds
     * @return
     */
    @DeleteMapping("deleteMedicinesByIds/{medicinesIds}")
    @Log(title = "删除药品信息", businessType = BusinessType.DELETE)
    @HystrixCommand
    public AjaxResult deleteMedicinesByIds(@Validated @PathVariable @NotNull(message = "药品medicinesIds不能为空") Long[] medicinesIds) {
        return AjaxResult.toAjax(this.medicinesService.deleteMedicinesByIds(medicinesIds));
    }

    /**
     * 查询全部可用的药品信息
     *
     * @return
     */
    @GetMapping("selectAllMedicines")
    @HystrixCommand
    public AjaxResult selectAllMedicines() {
        return AjaxResult.success(this.medicinesService.selectAllMedicines());
    }

    /**
     * 调整药品库存信息
     *
     * @return
     */
    @PostMapping("updateMedicinesStorage/{medicinesId}/{medicinesStockNum}")
    @Log(title = "调整药品库存信息", businessType = BusinessType.UPDATE)
    @HystrixCommand
    public AjaxResult updateMedicinesStorage(@PathVariable Long medicinesId, @PathVariable Long medicinesStockNum) {
        int i = this.medicinesService.updateMedicinesStorage(medicinesId, medicinesStockNum);
        return AjaxResult.toAjax(i);
    }
}
